package com.xy.p2p.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Date;
import com.xy.core.util.BeanUtil;
import com.xy.common.page.Pagination;
import com.xy.core.service.impl.BaseServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xy.p2p.dao.TmemberassetaccountDao;
import com.xy.p2p.dao.TmemberassetaccountdetailsDao;
import com.xy.p2p.dao.TmemberbankcardDao;
import com.xy.p2p.dao.TmemberratesDao;
import com.xy.p2p.dao.TwidrappDao;
import com.xy.p2p.model.Tmemberassetaccount;
import com.xy.p2p.model.Tmemberassetaccountdetails;
import com.xy.p2p.model.Tmemberrates;
import com.xy.p2p.model.Tuser;
import com.xy.p2p.model.Twidrapp;
import com.xy.p2p.service.TmemberassetaccountdetailsService;
import com.xy.p2p.service.TmemberbankcardService;
import com.xy.p2p.service.TmemberratesService;
import com.xy.p2p.service.TwidrappService;

@Service
@Transactional
public class TwidrappServiceImpl extends BaseServiceImpl<Twidrapp> implements
		TwidrappService {
	private final int TWIDRAPP_STATUS = 2;
	//1：成功，0：失败，2：我方处理中，3：第三方处理中
	private final int TWIDRAPP_AUDIT_SUCCESS=1;
	private final int TWIDRAPP_AUDIT_FAIL=0;
	
	@Autowired
	private TmemberassetaccountDao tmemberassetaccountDao;
	@Autowired
	private TmemberassetaccountdetailsDao tmemberassetaccountdetailsDao;
	@Autowired
	private TmemberbankcardDao tmemberbankcardDao; 
	@Autowired
	private TmemberratesService tmemberratesService;
	@Autowired
	private TmemberassetaccountdetailsService tmemberassetaccountdetailsService;
	
	private TwidrappDao twidrappDao;

	public TwidrappServiceImpl(TwidrappDao paramAppRoleDao) {
		super(paramAppRoleDao);
		this.twidrappDao = paramAppRoleDao;
	}

	@Override
	public boolean checkExist(String paraName, String paraValue) {
		return twidrappDao.checkExist("Twidrapp", paraName, paraValue);
	}

	@Override
	public List<Twidrapp> getTwidrapp(Map<String, Object> map) {
		return twidrappDao.getTwidrapp(map);
	}
	@Override
	public Pagination getTwidrappByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		return twidrappDao.getTwidrappByPage(map, pageNo, pageSize);
	}
	 
   @Override
	public Twidrapp save(Twidrapp twidrapp) {
//		twidrapp.setModtime(new Date());
		if(twidrapp.getWaid()==null){
			twidrapp = super.save(twidrapp);
        	return twidrapp;
		}else{
			//更新
			Twidrapp twidrapp1=twidrappDao.get(twidrapp.getWaid());
			try {
				BeanUtil.copyNotNullProperties(twidrapp1, twidrapp);
			} catch (Exception e) {
				e.printStackTrace();
			}  
			return super.save(twidrapp1);
		}
	}
   
   public String widrapp(Twidrapp twidrapp){
	 //新增
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("Tmember", twidrapp.getTmember());
		Tmemberassetaccount tmemberassetaccount = tmemberassetaccountDao.getTmemberassetaccount(queryMap).get(0);
		//判断有没有默认银行卡
		queryMap.put("iswithdraw", 1);
		if(tmemberbankcardDao.getTmemberbankcard(queryMap).size()<=0)
			return "没有默认银行卡.请到银行卡信息设置.";//没有默认银行卡
		//修改账户
		if(tmemberassetaccount.getBalance()<twidrapp.getWdam()+tmemberratesService.getWITHDRAW_FEE().getRates()+tmemberassetaccountdetailsService.getUnPayRechargeFee(twidrapp.getTmember()))
			return "余额不足.";//余额不足
		if(!Tmemberassetaccount.ABLE.equals(tmemberassetaccount.getStatus().toString())){
			return "账户状态异常, 请联系网站工作人员.";
		}
		//充值手续费
		double rechargeFee = tmemberassetaccountdetailsService.getUnPayRechargeFee(twidrapp.getTmember());
		//提现金额-提现手续费-充值手续费
		tmemberassetaccount.setBalance(tmemberassetaccount.getBalance()-(twidrapp.getWdam()+tmemberratesService.getWITHDRAW_FEE().getRates())-rechargeFee);
		tmemberassetaccount.setAfreason("提现处理中...");
		tmemberassetaccount.setAmountfrozen(tmemberassetaccount.getAmountfrozen()+twidrapp.getWdam()+tmemberratesService.getWITHDRAW_FEE().getRates()+rechargeFee);
		tmemberassetaccountDao.save(tmemberassetaccount);
		//增加交易明细
//		Tmemberassetaccountdetails tmemberassetaccountdetails = new Tmemberassetaccountdetails(twidrapp.getTmember(), twidrapp.getWdam(),
//				TMEMBERASSETACCOUNTDETAIL_BORROWING,TMEMBERASSETACCOUNTDETAIL_TRANTYPE , new Date(), "提现", null);
//		tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails);
		//新增提现申请
		twidrapp.setWdamdate(new Date());
		twidrapp.setStatus(TWIDRAPP_STATUS);
		twidrapp = super.save(twidrapp);
		return "提现申请已提交!.";
   }

   //提现审核
@Override
	public Twidrapp audit(Tuser tuser, String opr, Twidrapp twidrapp) {
		Map<String, Object> queryMap = new HashMap<String, Object>();
		queryMap.put("Tmember", twidrapp.getTmember());
		Tmemberassetaccount tmemberassetaccount = tmemberassetaccountDao.getTmemberassetaccount(queryMap).get(0);
		double rechargeFee = tmemberassetaccountdetailsService.getUnPayRechargeFee(twidrapp.getTmember());
		//通过审核
		if (opr.equals("y")) {
			twidrapp.setStatus(TWIDRAPP_AUDIT_SUCCESS);
			tmemberassetaccount.setAfreason(null);
			tmemberassetaccount.setAmountfrozen(tmemberassetaccount.getAmountfrozen() - (twidrapp.getWdam()+tmemberratesService.getWITHDRAW_FEE().getRates()) - rechargeFee);
			tmemberassetaccountDao.save(tmemberassetaccount);
			Tmemberassetaccountdetails tmemberassetaccountdetails = new Tmemberassetaccountdetails(twidrapp.getTmember(), twidrapp.getWdam(), Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_OUT,
				Tmemberassetaccountdetails.WITHDRAW, new Date(), "通联", null);
			tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails);
			Tmemberassetaccountdetails tmemberassetaccountdetails2 = new Tmemberassetaccountdetails(twidrapp.getTmember(), tmemberratesService.getWITHDRAW_FEE().getRates(), Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_OUT,
					Tmemberassetaccountdetails.WITHDRAW_FEE, new Date(), "平台", null);
			tmemberassetaccountdetailsDao.save(tmemberassetaccountdetails2);
			twidrapp.setTuser(tuser);
			twidrapp.setRevieweddate(new Date());
			//增加收取充值费用
			List<Tmemberassetaccountdetails> list = tmemberassetaccountdetailsService.getUnPayRechargeList(twidrapp.getTmember());
			for(Tmemberassetaccountdetails t : list){
				tmemberassetaccountdetailsDao.save(new Tmemberassetaccountdetails(twidrapp.getTmember(), (t.getTranamount()*tmemberratesService.getFee(Tmemberrates.RECHARGE_FEE).getRates()), Tmemberassetaccountdetails.TMEMBERASSETACCOUNTDETAIL_BORROWING_OUT, 
						Tmemberassetaccountdetails.RECHRAGE_FEE, new Date(), "平台", null));
				t.setRemark("已收取充值服务费");
				tmemberassetaccountdetailsDao.save(t);
			}
			//总充值金额
			double p = 0;
			for(Tmemberassetaccountdetails t : list){
				p += t.getTranamount();
			}
			if(0!=p){
				
			}
			//不通过审核↓↓↓↓↓↓↓
		} else {
			tmemberassetaccount.setBalance(tmemberassetaccount.getBalance() + twidrapp.getWdam()+tmemberratesService.getWITHDRAW_FEE().getRates()+rechargeFee);
			tmemberassetaccount.setAfreason(null);
			tmemberassetaccount.setAmountfrozen(tmemberassetaccount.getAmountfrozen() - twidrapp.getWdam()-tmemberratesService.getWITHDRAW_FEE().getRates() - rechargeFee);
			tmemberassetaccountDao.save(tmemberassetaccount);
			twidrapp.setStatus(TWIDRAPP_AUDIT_FAIL);
			twidrapp.setTuser(tuser);
			twidrapp.setRevieweddate(new Date());
		}
		return twidrappDao.save(twidrapp);
	}
}